package com.ruoyi.project.thirdservice.service.aliyun.logistics;

import com.alibaba.cloudapi.sdk.client.ApacheHttpClient;
import com.alibaba.cloudapi.sdk.enums.HttpMethod;
import com.alibaba.cloudapi.sdk.enums.ParamPosition;
import com.alibaba.cloudapi.sdk.model.ApiCallback;
import com.alibaba.cloudapi.sdk.model.ApiRequest;
import com.alibaba.cloudapi.sdk.model.ApiResponse;
import org.apache.commons.lang.StringUtils;

/**
 * @author yan
 */
public abstract class AliyunAbstractLogisticsApiClient extends ApacheHttpClient {

    final static String HOST = "ali-deliver.showapi.com";

    public void queryExpressCompany(String nu, ApiCallback callback) {
        String path = "/fetchCom";
        ApiRequest request = new ApiRequest(HttpMethod.GET, path);
        request.addParam("nu", nu, ParamPosition.QUERY, false);
        sendAsyncRequest(request, callback);
    }

    public ApiResponse queryExpressCompanySyncMode(String nu) {
        String path = "/fetchCom";
        ApiRequest request = new ApiRequest(HttpMethod.GET, path);
        request.addParam("nu", nu, ParamPosition.QUERY, false);
        return sendSyncRequest(request);
    }

    public void queryExpressNodeTrace(String com, String nu, String senderPhone, String receiverPhone, ApiCallback callback) {
        String path = "/showapi_expInfo";
        ApiRequest request = new ApiRequest(HttpMethod.GET, path);
        request.addParam("com", com, ParamPosition.QUERY, true);
        request.addParam("nu", nu, ParamPosition.QUERY, true);
        request.addParam("senderPhone",
                getPhoneNoLastFourDigitsIfNotBlank(senderPhone), ParamPosition.QUERY, false);
        request.addParam("receiverPhone",
                getPhoneNoLastFourDigitsIfNotBlank(receiverPhone), ParamPosition.QUERY, false);


        sendAsyncRequest(request, callback);
    }

    public ApiResponse queryExpressNodeTraceSyncMode(String com, String nu, String senderPhone, String receiverPhone) {
        String path = "/showapi_expInfo";
        ApiRequest request = new ApiRequest(HttpMethod.GET, path);
        request.addParam("com", com, ParamPosition.QUERY, true);
        request.addParam("nu", nu, ParamPosition.QUERY, true);
        request.addParam("senderPhone",
                getPhoneNoLastFourDigitsIfNotBlank(senderPhone), ParamPosition.QUERY, false);
        request.addParam("receiverPhone",
                getPhoneNoLastFourDigitsIfNotBlank(receiverPhone), ParamPosition.QUERY, false);


        return sendSyncRequest(request);
    }

    public void queryAllExpress(String expName, Long maxSize, Long page, ApiCallback callback) {
        String path = "/showapi_expressList";
        ApiRequest request = new ApiRequest(HttpMethod.GET, path);
        request.addParam("expName", expName, ParamPosition.QUERY, false);
        request.addParam("maxSize", String.valueOf(maxSize), ParamPosition.QUERY, false);
        request.addParam("page", String.valueOf(page), ParamPosition.QUERY, false);


        sendAsyncRequest(request, callback);
    }

    public ApiResponse queryAllExpressSyncMode(String expName, Long maxSize, Long page) {
        String path = "/showapi_expressList";
        ApiRequest request = new ApiRequest(HttpMethod.GET, path);
        request.addParam("expName", expName, ParamPosition.QUERY, false);
        request.addParam("maxSize", String.valueOf(maxSize), ParamPosition.QUERY, false);
        request.addParam("page", String.valueOf(page), ParamPosition.QUERY, false);

        return sendSyncRequest(request);
    }

    private String getPhoneNoLastFourDigitsIfNotBlank(String phone) {
        if (StringUtils.isNotBlank(phone)) {
            return phone.substring(phone.length() - 4);
        }
        return phone;
    }
}
